package threadLearn.producerconsumer.v2;

import java.util.Random;
import java.util.Vector;

public class Consumer implements Runnable{

	private Vector<String> queue;
	Random r = new Random();
	public Consumer(Vector<String> queue) {
		this.queue = queue;
	}
	
	@Override
	public void run() {
		
		while(true) {
			
			//如果队列为空，等待，通知生产者消费
			while(queue.size() <= 0) {
				synchronized (queue) {
					try {
						queue.wait();
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					queue.notifyAll();
					System.out.println("队列为空，等待生产者生产。。。");
				}
			}
			
			//消费者消费，队列移除面包
			synchronized (queue) {
				System.out.println(Thread.currentThread().getId()+"正在消费："+queue.get(0));
				queue.remove(0);
			}
		}
		
	}

}
